String.prototype.replaceAll = function(search, replace) {
    return this.split(search).join(replace);
}

// Array Remove - By John Resig (MIT Licensed)
/*Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};*/

//Arrays
function remove(array, from, to) {
    var rest = array.slice((to || from) + 1 || array.length);
    array.length = from < 0 ? array.length + from : from;
    return array.push.apply(array, rest);
}

function indexOf(array, el) {
    for (var i in array) {
        if (array[i] == el) return i;
    }
    return -1;
}

//Ajax
function makeRequest() {
    var request = null;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
            }
        }
    }
    return request;
}

function setResponseToElement(url, elementId) {
    var request = makeRequest();
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status != 200) {
                alert("Server error: " + request.statusText);
            } else {
                document.getElementById(elementId).innerHTML = request.responseText;
            }
        }
    };

    request.open("GET", url, true);
    request.send(null);
}

//Cookies
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function swichFavorite(name, element) {
    if(readCookie(name) == null) {
        createCookie(name, 1, 30);
        element.firstChild.src = '/img/favorite.png';
    } else {
        eraseCookie(name);
        element.firstChild.src = '/img/notfavorite.png';
    }
}


function xSetCookie(name, value, expire, path)
{
  document.cookie = name + "=" + escape(value) +
                    ((!expire) ? "" : ("; expires=" + expire.toGMTString())) +
                    "; path=" + ((!path) ? "/" : path);
}

function xGetCookie(name)
{
  var value=null, search=name+"=";
  if (document.cookie.length > 0) {
    var offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      var end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      value = unescape(document.cookie.substring(offset, end));
    }
  }
  return value;
}

function xDeleteCookie(name, path)
{
  if (xGetCookie(name)) {
    document.cookie = name + "=" +
                    "; path=" + ((!path) ? "/" : path) +
                    "; expires=" + new Date(0).toGMTString();
  }
}
